package jiang.web.controller.recruit;

import jiang.app.entity.Candidate;
import jiang.app.entity.Connection;
import jiang.app.entity.Exam;
import jiang.app.entity.Interview;
import jiang.app.entity.Review;
import jiang.app.service.StepService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class StepController {
	@Autowired
	StepService stepService;

	@RequestMapping(value = "/manage/recruit/step/{candidateId}")
	public String index(@PathVariable long candidateId, Model m) {
		// 目前求职者的状态
		Candidate c = stepService.findCandidateById(candidateId);
		int state = c.getState();
		m.addAttribute("candidate", c);

		switch (state) {
		// 录取
		case 6:
			break;
		// 终止
		case 5:
			break;		
		case 3:
			// 未面试阶段
			Interview interview = stepService.initInterviewCandidate(candidateId);
			m.addAttribute("interview", interview);
		case 2:
			// 未笔试阶段
			Exam exam = stepService.initExamCandidate(candidateId);
			m.addAttribute("exam", exam);
		case 1:
			// 未联系求职者阶段
			Connection connection = stepService.initConnectionCandidate(candidateId);
			m.addAttribute("connection", connection);
		case 0:
			// 未初步审核
			Review review = stepService.initReviewCandidate(candidateId);
			m.addAttribute("review", review);
		}
		return "recruit/step";
	}

	@RequestMapping(value = "/manage/recruit/step/review", method = RequestMethod.POST)
	public String review(Review r, Model m, long candidateId) {
		try{
			stepService.updateReviewCandidate(r);
		}
		catch(Exception e){
			e.printStackTrace();
			m.addAttribute("tip", "提交失败");
		}
		stepService.updateCandidateState_Review(candidateId, r.getReviewState());
		m.addAttribute("tip", "提交成功");
		return "recruit/result";
	}
	
	@RequestMapping(value = "/manage/recruit/step/connect", method = RequestMethod.POST)
	public String connect(Connection c, Model m, long candidateId) {
		try{
			stepService.updateConnectionCandidate(c);
		}
		catch(Exception e){
			e.printStackTrace();
			m.addAttribute("tip", "提交失败");
		}
		stepService.updateCandidateState_Connection(candidateId, c.getConnectState());
		m.addAttribute("tip", "提交成功");
		return "recruit/result";
	}
	
	@RequestMapping(value = "/manage/recruit/step/exam", method = RequestMethod.POST)
	public String exam(Exam exam, Model m, long candidateId) {
		try{
			stepService.updateExamCandidate(exam);
		}
		catch(Exception e){
			e.printStackTrace();
			m.addAttribute("tip", "提交失败");
		}
		stepService.updateCandidateState_Exam(candidateId, exam.getExamState());
		m.addAttribute("tip", "提交成功");
		return "recruit/result";
	}
	
	@RequestMapping(value = "/manage/recruit/step/interview", method = RequestMethod.POST)
	public String exam(Interview interview, Model m, long candidateId) {
		try{
			stepService.updateInterviewCandidate(interview);
		}
		catch(Exception e){
			e.printStackTrace();
			m.addAttribute("tip", "提交失败");
		}
		stepService.updateCandidateState_Interview(candidateId, interview.getInterviewState());
		m.addAttribute("tip", "提交成功");
		return "recruit/result";
	}
}
